package de.fhffm.scs.simulator.gui.composites;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Scale;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import de.fhffm.scs.simulator.gui.SimulationAppender;

public class SimulationController extends Composite {

	public SimulationController(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));

		Group grpSimulationControl = new Group(this, SWT.NONE);
		grpSimulationControl.setLayout(new GridLayout(1, false));
		grpSimulationControl.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
				true, true, 1, 1));
		grpSimulationControl.setText("Simulation Control");

		Button btnStart = new Button(grpSimulationControl, SWT.NONE);
		btnStart.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		btnStart.setText("Start");

		Button btnStop = new Button(grpSimulationControl, SWT.NONE);
		btnStop.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		btnStop.setText("Stop");

		Scale scale = new Scale(grpSimulationControl, SWT.NONE);
		scale.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Label lblBodyController = new Label(grpSimulationControl, SWT.SEPARATOR
				| SWT.HORIZONTAL);
		lblBodyController.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));
		lblBodyController.setText("Body Controller");

		Button btnFeed = new Button(grpSimulationControl, SWT.NONE);
		btnFeed.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		btnFeed.setText("Feed");

		List log = new List(grpSimulationControl, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		log.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		

		wrapWithCustomAppender(log);
	}

	private void wrapWithCustomAppender(List text) {
		SimulationAppender appender = new SimulationAppender(text);
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		Logger logger = lc.getLogger("de.fhffm.scs.simulator");
		logger.setLevel(Level.INFO);
		appender.setContext(logger.getLoggerContext());
		logger.addAppender(appender);
		appender.start();
	}
}
